/***
 * Excerpted from "Stripes: and Java Web Development is Fun Again",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/fdstr for more book information.
 ***/
package stripesbook.action.user;

import java.util.List;
import javax.annotation.security.RolesAllowed;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.HttpCache;
import net.sourceforge.stripes.action.LocalizableMessage;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import stripesbook.action.BaseActionBean;
import stripesbook.model.Role;
import stripesbook.model.User;
import stripesbook.view.Menu;
import stripesbook.view.UserListForm;

@RolesAllowed("Administrator")
@HttpCache(allow = false)
public class UserListActionBean extends BaseActionBean {
  @Override
  public Menu.Section getCurrentSection() {
    return Menu.Section.UserList;
  }

  @DefaultHandler
  public void list() {
  }

  public Resolution save() {
    for (User user : users) {
      userDao.save(user);
    }
    userDao.commit();
    getContext().getMessages().add(new LocalizableMessage("userList.saved"));
    return new RedirectResolution(getClass());
  }

  private List<User> users = userDao.read();

  public List<User> getUsers() {
    return users;
  }

  public void setUsers(List<User> users) {
    this.users = users;
  }

  public List<Role> getRoles() {
    return roleDao.read();
  }

  public Object getForm() {
    return tag(UserListForm.class).users(getUsers(), getRoles());
  }

}
